﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils;
using System.Windows.Forms;
using MM.Common.Base;
using System.Data;
using DevExpress.XtraEditors.Drawing;

namespace MM.Common.UI.Controls
{
    public class UCComboData : LookUpEdit, IControlProperty
    {
        // Fields
        private string _ControlCode;
        private string _ControlName;
        private EControlPropertyStatus _ControlProperty;
        protected string _displayColumn = "";
        private bool _existInputCode;
        protected bool _ExistListData;
        protected string _inputcode1Column = "INPUTCODE1";
        protected string _inputcode2Column = "INPUTCODE2";
        private IContainer components;
        protected UCComboBoxInputItems itemColumns;

        // Methods
        public UCComboData()
        {
            this.InitializeComponent();
            base.Properties.NullText = "";
            this._displayColumn = base.Properties.DisplayMember;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public virtual void Init()
        {
            if ((this.itemColumns != null) && (this.itemColumns.Count != 0))
            {
                this.ExistInputCode = false;
                base.Properties.Columns.Clear();
                for (int i = 0; i < this.itemColumns.Count; i++)
                {
                    if (this.itemColumns[i].ItemType == UCComboBoxInputItemType.Data)
                    {
                        base.Properties.ValueMember = this.itemColumns[i].FieldName.ToUpper();
                    }
                    if (this.itemColumns[i].ItemType == UCComboBoxInputItemType.Text)
                    {
                        base.Properties.DisplayMember = this.itemColumns[i].FieldName.ToUpper();
                        this._displayColumn = base.Properties.DisplayMember;
                    }
                    if (this.itemColumns[i].ItemType == UCComboBoxInputItemType.InputCode1)
                    {
                        this._inputcode1Column = this.itemColumns[i].FieldName.ToUpper();
                        this.ExistInputCode = true;
                    }
                    if (this.itemColumns[i].ItemType == UCComboBoxInputItemType.InputCode2)
                    {
                        this._inputcode2Column = this.itemColumns[i].FieldName.ToUpper();
                        this.ExistInputCode = true;
                    }
                    base.Properties.Columns.Add(new LookUpColumnInfo(this.itemColumns[i].FieldName.ToUpper(), this.itemColumns[i].ItemWidth, this.itemColumns[i].ItemName));
                }
                base.Properties.CloseUpKey = new KeyShortcut(Keys.Shift | Keys.F4);
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnClickButton(EditorButtonObjectInfoArgs buttonInfo)
        {
            base.OnClickButton(buttonInfo);
            if (!this.IsPopupOpen)
            {
                if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
                {
                    SendKeys.Send("{F4}");
                }
                else
                {
                    SendKeys.Send("+{F4}");
                }
            }
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            if ((!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty != EControlPropertyStatus.Write)) && base.Enabled)
            {
                base.Enabled = false;
            }
        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            if (this._existInputCode && !base.Properties.ReadOnly)
            {
                this._displayColumn = base.Properties.DisplayMember;
                if (SystemInfo.OperInputCode == "1")
                {
                    base.Properties.DisplayMember = this._inputcode1Column;
                }
                else
                {
                    base.Properties.DisplayMember = this._inputcode2Column;
                }
            }
            if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
            {
                SendKeys.Send("{F4}");
            }
            else
            {
                SendKeys.Send("+{F4}");
            }
            DataTable dataSource = base.Properties.DataSource as DataTable;
            if (dataSource != null)
            {
                this._ExistListData = dataSource.Rows.Count > 0;
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if ((!this.IsPopupOpen && !e.Shift) && (this._ExistListData && !base.Properties.ReadOnly))
            {
                string str = char.ConvertFromUtf32(e.KeyValue);
                if (char.IsLetterOrDigit(str[0]) && (str.Length == 1))
                {
                    SendKeys.Send("{F4}");
                }
                if ((str == "\r") && this.EnterMoveNextControl)
                {
                    SendKeys.Send("{TAB}");
                }
            }
            base.OnKeyDown(e);
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            if (this._existInputCode && !base.Properties.ReadOnly)
            {
                base.Properties.DisplayMember = this._displayColumn;
            }
        }

        protected override void OnLoaded()
        {
            base.OnLoaded();
            this.Init();
            base.PropertiesChanged += new EventHandler(this.UCComboData_PropertiesChanged);
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if ((!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty == EControlPropertyStatus.Hide)) && base.Visible)
            {
                base.Visible = false;
            }
        }

        private void UCComboData_PropertiesChanged(object sender, EventArgs e)
        {
            if ((((e is PropertyChangedEventArgs) && (((PropertyChangedEventArgs)e).PropertyName == "ReadOnly")) && (!string.IsNullOrEmpty(this.ControlCode) && (this.ControlProperty != EControlPropertyStatus.Write))) && !base.Properties.ReadOnly)
            {
                base.Properties.ReadOnly = true;
            }
        }

        // Properties
        [DefaultValue((string)null), Browsable(true), Category("MM附加"), Description("控制本控件对应的系统代码")]
        public string ControlCode
        {
            get
            {
                return this._ControlCode;
            }
            set
            {
                this._ControlCode = value;
            }
        }

        [Category("MM附加"), DefaultValue((string)null), Browsable(true), Description("控制本控件对应的系统名称")]
        public string ControlName
        {
            get
            {
                return this._ControlName;
            }
            set
            {
                this._ControlName = value;
                if (!base.DesignMode && !string.IsNullOrEmpty(this.ControlCode))
                {
                    if (this._ControlProperty == EControlPropertyStatus.None)
                    {
                        this._ControlProperty = BaseDataHelper.GetFunctionPointProperty(this._ControlCode, this._ControlName);
                    }
                    switch (this._ControlProperty)
                    {
                        case EControlPropertyStatus.Read:
                            base.Properties.ReadOnly = true;
                            base.Visible = true;
                            return;

                        case EControlPropertyStatus.Write:
                            base.Properties.ReadOnly = false;
                            base.Visible = true;
                            return;
                    }
                    base.Visible = false;
                }
            }
        }

        [DefaultValue(0), Browsable(false)]
        public EControlPropertyStatus ControlProperty
        {
            get
            {
                return this._ControlProperty;
            }
            set
            {
            }
        }

        [Category("MM附加"), Description("是否存在输入码"), DefaultValue(false), Browsable(true)]
        public bool ExistInputCode
        {
            get
            {
                return this._existInputCode;
            }
            set
            {
                this._existInputCode = value;
                if (this._existInputCode)
                {
                    base.Properties.CloseUpKey = new KeyShortcut(Keys.Shift | Keys.F4);
                }
            }
        }

        [Browsable(true), Category("MM附加"), Description("项目列信息"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UCComboBoxInputItems ItemColumns
        {
            get
            {
                if (this.itemColumns == null)
                {
                    this.itemColumns = new UCComboBoxInputItems();
                }
                return this.itemColumns;
            }
            set
            {
                this.itemColumns = value;
            }
        }
    }
}
